package cn.net.autocode.platform.roles;

import cn.net.autocode.core.bean.ResultT;
import cn.net.autocode.core.utils.UserThreadLocalUtils;
import cn.net.autocode.dbManager.Dao;
import cn.net.autocode.dbManager.IdWorker;
import cn.net.autocode.platform.roles.service.RoleServ;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/platform/roles")
public class PlatformRoleCtrl {

    private final Dao dao;
    private final RoleServ roleServ;

    public PlatformRoleCtrl(Dao dao, RoleServ roleServ) {
        this.dao = dao;
        this.roleServ = roleServ;
    }


    @RequestMapping("savePublicRole")
    public ResultT savePublicRole(@RequestBody JSONObject param){
        if(param.getString("id") == null || param.getString("id").isBlank()){
            param.put("id",IdWorker.ulid());
            param.put("isPublic","Y");
            param.put("orgCode","default");
            dao.createJSON(param,"sys_roles");
        }else{
            param.put("isPublic","Y");
            param.put("orgCode","default");
            dao.updateJSON(param,"sys_roles");
        }
        return ResultT.success(param);
    }

    @RequestMapping("savePublicRoleMenus")
    public ResultT savePublicRoleMenus(@RequestBody JSONObject param){
        roleServ.saveRoleMenus(param);
        return ResultT.success();
    }

    @RequestMapping("savePublicAppRoleMenus")
    public ResultT savePublicAppRoleMenus(@RequestBody JSONObject param){
        roleServ.saveAppRoleMenus(param);
        return ResultT.success();
    }


    @RequestMapping("saveRole")
    public ResultT saveRole(@RequestBody JSONObject param){
        String orgCode = UserThreadLocalUtils.getOrgCode();
        if(param.getString("id") == null || param.getString("id").isBlank()){
            param.put("id",IdWorker.ulid());
            param.put("isPublic","N");
            param.put("orgCode",orgCode);
            dao.createJSON(param,"sys_roles");
        }else{
            param.put("isPublic","N");
            param.put("orgCode",orgCode);
            dao.updateJSON(param,"sys_roles");
        }
        return ResultT.success(param);
    }

    @RequestMapping("saveRoleMenus")
    public ResultT saveRoleMenus(@RequestBody JSONObject param){

        String roleId = param.getString("roleId");
        String isPublic = dao.queryValue("select is_public from sys_roles where id=?",String.class,roleId);
        if(isPublic.equals("Y")){
            throw new RuntimeException("您不能修改公共菜单的操作权限");
        }

        roleServ.saveRoleMenus(param);
        return ResultT.success();
    }


    @RequestMapping("saveAppRoleMenus")
    public ResultT saveAppRoleMenus(@RequestBody JSONObject param){

        String roleId = param.getString("roleId");
        String isPublic = dao.queryValue("select is_public from sys_roles where id=?",String.class,roleId);
        if(isPublic.equals("Y")){
            throw new RuntimeException("您不能修改公共菜单的操作权限");
        }
        roleServ.saveAppRoleMenus(param);
        return ResultT.success();
    }
}
